Bash script to create sequential, numbered directories
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash script to create sequential, numbered directories
Hi, I am working on a backup system which needs to create numbered sub dirs for each run. For example, when first run it creates one called "1", next time "2" etc. So it needs to detect how many of these numbered dirs are present, increment that count and call mkdir using it. Thanks.
I'd suggest to write numbered directories padded with zeros, e.g. 0001, 0002, 0003... instead of 1, 2, 3. In this way they will be sorted sequentially.
It also will be simplier to check the last one and create the new directory at each run. For example:
Code:
#!/bin/bash
# -------------------------------------------------------
# find last created directory: format NNNN
# -------------------------------------------------------
lastdir=$(ls -d [0-9][0-9][0-9][0-9] | tail -1)
# -------------------------------------------------------
# create new sequential directory
# -------------------------------------------------------
newdir=$((++lastdir))
mkdir $(printf "%04u" $newdir)
Last edited by colucix; 01-18-2008 at 08:00 AM.
Reason: removed unuseful newline character from the printf format string
I am perplexed. I have a situation where I have a folder named parts. In parts I have numerous subfolders labeled sprocket1, sprocket2, spoke1, spoke2, spoke3, rim1, rim2, etc.
I need to occasionally create new sequential subfolders via a bash script. I can't seem to adjust either of the scripts in this thread to do that.
Let's say I need to create a sequential spoke subfolder. As listed above, we know that would be spoke3. But let's say we don't know what the sequence number is for the last created spoke subfolder. How can I determine that via bash and create a new subfolder in sequence?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.